<?php

namespace App\Http\Controllers\Index;

use App\Facades\Site;
use App\Jobs\SendEmail;
use App\Jobs\SendReminderEmail;
use App\Models\Admin\AdManage;
use App\Models\Admin\AdSite;
use App\Models\Admin\AreaStation;
use App\Models\Admin\Links;
use App\Models\Common\CollArea;
use App\Models\Common\CollOption;
use App\Models\Common\CollPosition;
use App\Models\Company\CompanyJob;
use App\Models\Company\Points;
use App\Models\Personal\BrowseResume;
use App\Models\Personal\Interview;
use App\Models\Personal\ResumeSend;
use App\Models\News\NewsCategory;
use App\Models\News\News;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Style;


//require_once(app_path('/Repositories/Eloquent/YiwangAccessToken.php'));

class IndexController extends CommonController{

    use TemplateIndex;

    /**
     * 测试页
     * @return string
     */
    public function test(Request $request){
        session()->put('category', '');
      //  dd(session('category'));
        /**清理垃圾注册
        $companyUser=CompanyUser::where('contacts','思考思考')->where('updatetime','>','2017-08-07 01:01:01')->limit(1000)->get();
        foreach($companyUser as $key=>$value){
            CompanyVisitRecord::where('uid',$value->uid)->delete();
            CompanyLoginRecord::where('uid',$value->uid)->delete();
            CompanyBasic::where('uid',$value->uid)->delete();
            CompanyUser::where('uid',$value->uid)->delete();
        }
        exit('完成'.count($companyUser));
        * **/
    }


    /**
     * 默认首页控制器
     */
    public function index(Request $request){
        if(Site::get('template') != '' && method_exists($this, Site::get('template') . 'Index')){
            return $this->{Site::get('template') . 'Index'}($request);
        }
        return $this->defaultIndex($request);
    }


/**
     * 一网cms首页
     * @return mixed
     */
    protected function defaultIndex($request){
        //获取当前URL域名地址
        $url = getUrl();
        //默认主站的标识和编号
        $station_sign = 'main';
        $station_cid = '';
        $number = 9;//默认9
        $station = AreaStation::
        where(function($query) use ($url){
            if(!empty($url)){
                $query->orWhere('station_domain', '=', $url)
                    ->orWhere('station_domain', '=', $url . '/');
            }
        })
            ->first();
        if(!empty($station)){
            $station_sign = $station->station_sign;
            $station_cid = $station->station_cid;
            $number = 18;
        }
        //职位分类
        $position = CollPosition::getFirstPosition();
        $positionAllThree = CollPosition::getAllThree();
        //检查用户是不是登录
        $interview_count = 0;
        $browse_resume_count = 0;
        if(auth()->guard('personal')->check()){
            $user = auth()->guard('personal')->user();
            //面试邀请数量
            $interview_count = Interview::where('personal_uid', $user->uid)->where('status', '!=', 4)->count();
            //谁看过我数量
            $browse_resume_count = BrowseResume::where('personal_uid', $user->uid)->count();
        }elseif(auth()->guard('company')->check()){
            $company_user = auth()->guard('company')->user();
            //查收简历
            $resumeSendCount = ResumeSend::where('company_uid', $company_user->uid)->where('status', '!=', 4)->count();
            $companyJobCount = CompanyJob::where('uid', $company_user->uid)->where('job_status', '!=', 4)->count();
        }

        //获取职位列表(今日高薪)
        $jobList = DB::table('company_job as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->join('company_users as t3', 't1.uid', '=', 't3.uid')
            ->select('t1.id', 't1.com_id', 't1.salary_above', 't1.urgent', 't1.salary_below', 't1.job_title', 't2.company_name')
            //工作地点(某个地区下)
            ->where(function($query) use ($station_cid){
                if(!empty($station_cid)){
                    $query->orWhere('t1.location1', '=', $station_cid)
                        ->orWhere('t1.location2', '=', $station_cid)
                        ->orWhere('t1.location3', '=', $station_cid)
                        ->orWhere('t1.location4', '=', $station_cid)
                        ->orWhere('t1.location5', '=', $station_cid);
                }
            })
            ->where('t1.job_status', '1')
            ->where('t3.viptime', '>=', date('Y-m-d H:i:s', time()))
            ->where('salary_above', '>=', '5000')
            ->orderBy('t1.updatetime', 'desc')->take(6)->get();
        $jobList = $this->jobInfo($jobList);

        /*@param 获取广告信息
         * 第一参数 广告位站点标识
         * 第二参数 广告位位置标识
         */
        //登录框banner广告
        $loginT1 = AdManage::getAd($station_sign, 'T1');

        //登录框显示职位广告
        $loginT2 = AdManage::getAd($station_sign, 'T2');
        if(!empty($loginT2)){
            foreach($loginT2 as $v){
                if(!empty($v->com_id)){
                    //获取加急职位
                    $companyJob = CompanyJob::where('com_id', $v->com_id)->where('job_status', '1')
                        ->orderBy('urgent', 'desc')->select('job_title')->get();
                    if(count($companyJob) > 0){
                        $job_title = '';
                        foreach($companyJob as $vv){
                            $job_title .= $vv->job_title . '、';
                        }
                        $v->job_title = trim($job_title, '、');
                    }else{
                        $v->job_title = '';
                    }
                }else{
                    $v->job_title = '';
                }
            }
        }
        //登录框下方广告
        $loginT3 = AdManage::getAd($station_sign, 'T3');
        //首页名企 默认为台州站
        $mingP = AdManage::getAd($station_sign, 'M1');
        //首页中部半长条
        $half = AdManage::getAd($station_sign, 'P1');
        //首页中部短条
        $short = AdManage::getAd($station_sign, 'P2');
        //友情链接获取
        $links = Links::getLinks($station_sign);
        //默认强烈推荐急招 显示不限
        $recommend = DB::table('company_job as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->select('t1.id', 't1.com_id', 't1.salary_above', 't1.salary_below', 't1.job_title', 't1.location1', 't1.updatetime', 't1.urgent', 't2.company_name')
            //工作地点(某个地区下)
            ->where(function($query) use ($station_cid){
                if(!empty($station_cid)){
                    $query->orWhere('t1.location1', '=', $station_cid)
                        ->orWhere('t1.location2', '=', $station_cid)
                        ->orWhere('t1.location3', '=', $station_cid)
                        ->orWhere('t1.location4', '=', $station_cid)
                        ->orWhere('t1.location5', '=', $station_cid);
                }
            })
            //职位显示审核通过的
            ->where('t1.job_status', '1')
            ->orderBy('t1.urgent', 'desc')
            ->orderBy('t1.updatetime', 'desc')
            ->take($number)
            ->get();
        $recommend = $this->jobInfo($recommend);

        //分类类别查询
        //销售/营销/贸易/外贸 职位急招
        $sellCateArr = ['11', '24'];
        $sellCate = $this->cateSelect($sellCateArr, $station_cid);
        //文职/行政/客服类
        $civilianCateArr = ['12'];
        $civilianCate = $this->cateSelect($civilianCateArr, $station_cid);
        //餐饮/百货/生活服务
        $foodCateArr = ['20'];
        $foodCate = $this->cateSelect($foodCateArr, $station_cid);
        //广告/设计/传媒
        $advertisingCateArr = ['19'];
        $advertisingCate = $this->cateSelect($advertisingCateArr, $station_cid);
        //能源/化工/服装
        $energyCateArr = ['23'];
        $energyCate = $this->cateSelect($energyCateArr, $station_cid);

        //By 小周 讯息分类
        $firstClass = NewsCategory::select('id')->where('new_sign', 'news')->first();
        if(!empty($firstClass)){
            $secondClass = NewsCategory::select('id', 'defectsname')->where('pid', $firstClass->id)->where('level', 1)->take(8)->get();
        }else{
            $secondClass =(object)array();
        }
        //网站公告
        $siteNotice = DB::table('news as t1')
            ->join('news_category as t2', 't1.category', '=', 't2.id')
            ->select('t1.id', 't1.title', 't2.new_sign')
            ->where('t2.new_sign', 'web_announcement')
            ->orderBy('updated_at', 'desc')->take(5)->get();
        //简历指导+面试技巧
        $resumeAndInterview = DB::table('news as t1')
            ->join('news_category as t2', 't1.category', '=', 't2.id')
            ->select('t1.id', 't1.title', 't2.new_sign')
            ->where('t2.new_sign', 'guidance')->orWhere('t2.new_sign', 'interview')
            ->orderBy('updated_at', 'desc')->take(5)->get();
        //事业单位
        $institution = DB::table('news as t1')
            ->join('news_category as t2', 't1.category', '=', 't2.id')
            ->select('t1.id', 't1.title', 't2.new_sign')
            ->where('t2.new_sign', 'Institutions')
            ->orderBy('updated_at', 'desc')->take(5)->get();

        if(!empty($station_sign) && $station_sign != 'main'){
            return Style::view('index.stationIndex', compact('position', 'positionAllThree', 'jobList', 'interview_count', 'browse_resume_count',
                'resumeSendCount', 'companyJobCount',
                'loginT1', 'loginT2', 'loginT3', 'mingP', 'half', 'short', 'links', 'recommend',
                'sellCate', 'civilianCate', 'foodCate', 'advertisingCate', 'energyCate',
                'secondClass', 'siteNotice', 'resumeAndInterview', 'institution', 'station_cid', 'station'));
        }else{
            return Style::view('index.index', compact('position', 'positionAllThree', 'jobList', 'interview_count', 'browse_resume_count',
                'resumeSendCount', 'companyJobCount',
                'loginT1', 'loginT2', 'loginT3', 'mingP', 'half', 'short', 'links', 'recommend',
                'sellCate', 'civilianCate', 'foodCate', 'advertisingCate', 'energyCate',
                'secondClass', 'siteNotice', 'resumeAndInterview', 'institution'));
        }
    }


    /**
     * 名企广告加载
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function mingP(Request $request){
        $adSite = AdSite::where('id', $request->id)->first(['site_sign']);
        $ming_p = AdManage::getAd($adSite->site_sign, 'M1');
        if($ming_p){
            return response()->json(['status' => 0, 'comanylogo' => $ming_p]);
        }else{
            return response()->json(['status' => 1]);
        }
    }

    /**
     * ajax加载急招
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function urgent(Request $request){
        //强烈推荐急招
        $company_job = DB::table('company_job as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->select(
                't1.id', 't1.com_id', 't1.salary_above', 't1.salary_below', 't1.job_title', 't1.urgent',
                't2.company_name'
            )
            ->where(function($query) use ($request){
                if(!empty($request->location)){
                    $query->where('t1.location1', $request->location)
                        ->orWhere('t1.location2', $request->location)
                        ->orWhere('t1.location3', $request->location)
                        ->orWhere('t1.location4', $request->location)
                        ->orWhere('t1.location5', $request->location);
                }
            })
            //职位显示审核通过的
            ->where('t1.job_status', '1')
            ->orderBy('t1.urgent', 'desc')
            ->orderBy('t1.updatetime', 'desc')
            ->take(9)
            ->get();
        $company_job = $this->jobInfo($company_job);
        return response()->json($company_job);
    }

    /**
     * 企业注册
     * @return mixed
     */
    public function reg(){
        return Style::view('company.auth.register');
    }

    /**
     * 企业注册
     * @return mixed
     */
    public function smartRefresh(){
        $points = Points::where('uptime', '<', date('Y-m-d H:i:s', time()))->select('com_uid', 'id', 'uptime')->get();
        if(count($points) > 0){
            $arr = [];
            foreach($points as $v){
                CompanyJob::where('uid', $v->com_uid)->update(['updatetime' => $v->uptime]);
                $arr[] = $v->id;
            }
            Points::whereIn('id', $arr)->delete();
        };
        ob_start();
        $im = imagecreate(1, 1);
        ImageGif($im);
        ImageDestroy($im);
        $content = ob_get_clean();
        return response($content, 200, [
            'Content-Type' => 'image/gif',
        ]);
    }

    /**
     * 设置App session值
     * @param Request $request
     */
    public function personalSession(Request $request){
        $request->session()->put('personal_session', 1);
    }

    /**
     * 设置App session值
     * @param Request $request
     */
    public function companySession(Request $request){
        $request->session()->put('company_session', 1);
    }
}